package com.example.graduate.Service.ServiceImpl;

import com.example.graduate.Bean.*;
import com.example.graduate.Service.CardsCollectService;
import com.example.graduate.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service(value = "CardsCollectService")
public class CardsCollectServiceImpl implements CardsCollectService {


    @Autowired
    private CardsCollectMapper cardsCollectmapper;
    @Autowired
    private UserCardsMapper userCardsMapper;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardsCardMapper cardsCardMapper;
    @Autowired
    private ClientMapper clientMapper;


    @Override
    public CardsCollect selectCards(Integer id) {
        return cardsCollectmapper.selectCards(id);
    }

    @Override
    public Card getCardById(Integer id) {
        return cardMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CardsCollect> selectCardsCollect(Integer id) {
        return cardsCollectmapper.selectCardsCollect(id);
    }

    @Override
    public void inser(CardsCollect cardsCollect) {
        cardsCollect.setCreattime(new Date());
        //这里再xml时候设置了自增长的id是什么，所以会自动赋值给这个实体类id
        cardsCollectmapper.insert(cardsCollect);
        UserCards userCards=new UserCards();
        userCards.setUserid(Integer.valueOf(cardsCollect.getCreatby()));
        //这里直接获取cardsCollect的id就好
        userCards.setCardsid(cardsCollect.getId());
        userCardsMapper.insert(userCards);

        //接下来插入卡片数据
        for(int i=0;i<cardsCollect.getCardList().size();i++){
            cardMapper.insert(cardsCollect.getCardList().get(i));
            CardsCard cardsCard=new CardsCard();
            cardsCard.setCardsid(cardsCollect.getId());
            cardsCard.setCardid(cardsCollect.getCardList().get(i).getId());
            cardsCardMapper.insert(cardsCard);
        }

    }

    @Override
    public void insertCard(Card card) {
        cardMapper.insert(card);
    }

    @Override
    public int updateCardBy(Card card) {
        return cardMapper.updateByPrimaryKey(card);
    }


    //改变用户当前正在学习卡片
    @Override
    public void updateLearnCards(Client client) {
        clientMapper.updateByPrimaryKeySelective(client);
    }

    @Override
    public int deleteCardsById(Integer id,Integer userId) {
        return userCardsMapper.deleteById(id,userId);
    }

    @Override
    public int deleteCardById(Integer id) {
        return cardsCardMapper.deleteCardById(id);
    }

    @Override
    public int insertCardsCard(CardsCard cardsCard) {
        return cardsCardMapper.insert(cardsCard);
    }

    @Override
    public List<CardsCollect> selectAllCards() {
        return cardsCollectmapper.selectAllCards();
    }


}
